﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;


namespace MonsterNet
{
	/// <summary>
	///	Represents a File
	/// Contains infomation about its synchronisation status.
	/// </summary>
	
	[Serializable]
	public class MNSynchronisedFile : MNSynchronisedFSObject, ISerializable
    {
        #region variables

        private string _extension;
        private ulong[] _sizeInBytes;

        #endregion

        #region getters/setters
        public string Extension
        {
            get { return _extension; }
        }
		
		/*
		public ulong[] SizeInBytes
		{
			get { return _sizeInBytes; }
			set { _sizeInBytes = value; }
		}*/
		
        public ulong GetSizeInBytesForLocation(MNFSObjectLocation location)
        {
			return _sizeInBytes[(int)location];
		}
		
		public void SetSizeInBytesForLocation(MNFSObjectLocation location, ulong size) {
			_sizeInBytes[(int)location] = size;
		}
			
		
		public string GetSizeAsStringForLocation(MNFSObjectLocation location)
		{
			return MNGlobal.GetSizeFromUlongToString(_sizeInBytes[(int)location]);
		}
		
		public bool GetIsSynchronised() {
			return this.GetExistsAtForLocation(MNFSObjectLocation.Source) && this.GetExistsAtForLocation(MNFSObjectLocation.Destination);
		}
		
        #endregion

        #region Constructor
		
		public MNSynchronisedFile () : base ()
		{
			_sizeInBytes = new ulong[(int)MNFSObjectLocation.NumberOfLocations];
		}
		
        public MNSynchronisedFile(FileInfo newFile, string rootPath, 
		                          MNFSObjectLocation location, DateTime foundDateTime) : this()	{
            int indexLocation = (int)location;          

            //set file info.
            _name = newFile.Name;
            _extension = newFile.Extension;
            _rootlessPath = MNGlobal.CreateRootlessPath(newFile.DirectoryName , rootPath);
            _dateFound[indexLocation] = foundDateTime;
            _dateModified[indexLocation] = newFile.LastWriteTime;
            _existsAt[indexLocation] = true;            
            _sizeInBytes[indexLocation] = (ulong)newFile.Length;
			//set all new files at the destination to private
			/*if(location == MNFSObjectLocation.Destination) {
				_private = true;
			}*/		
        }
		
		public MNSynchronisedFile(FileInfo newFile, string rootlessPath, 
		                          MNFSObjectLocation location, DateTime foundDateTime,
		                         bool privateFile) : this(newFile, rootlessPath, location, foundDateTime)	{
			_private = privateFile;			
		}
		
        #endregion
		
		#region Serialization
		
		private const string kSerialiseExtension = "e";
		private const string kSerialiseSizeInBytes = "s";
		
		public MNSynchronisedFile(SerializationInfo info, StreamingContext ctxt) : base (info, ctxt) {	
			_extension = info.GetString(kSerialiseExtension);
			_sizeInBytes = (ulong[])info.GetValue(kSerialiseSizeInBytes,typeof(ulong[]));
		}
		
		public new void GetObjectData(SerializationInfo info, StreamingContext ctxt) {
			base.GetObjectData(info, ctxt);
			
	    	info.AddValue(kSerialiseExtension, _extension);
			info.AddValue(kSerialiseSizeInBytes, _sizeInBytes);	
		}		
		#endregion			
		
        public void Update(FileInfo file, MNFSObjectLocation location, DateTime scanDateTime) {
            _dateModified[(int)location] = file.LastWriteTime;
            _existsAt[(int)location] = true;
            _sizeInBytes[(int)location] = (ulong)file.Length;
        }	
    }
}
